package plugins

import (
	"github.com/go-oauth2/oauth2/v4/manage"
	"github.com/go-oauth2/oauth2/v4/models"
	"github.com/go-oauth2/oauth2/v4/store"
	"lib/mall/v1/models/userModels"
)

var manager *manage.Manager

func init() {
	manager = manage.NewDefaultManager()
	manager.SetClientTokenCfg(manage.DefaultPasswordTokenCfg)
	manager.SetRefreshTokenCfg(manage.DefaultRefreshTokenCfg)
	// token memory store
	manager.MustTokenStorage(store.NewMemoryTokenStore())

	// client memory store
	clientStore := store.NewClientStore()
	var oClient []*userModels.OAuthClient
	Db().Find(&oClient)
	for _, item := range oClient {
		clientStore.Set(item.Code, &models.Client{
			ID:     item.Code,
			Secret: item.Secret,
			Domain: item.Domain,
		})
	}

	manager.MapClientStorage(clientStore)
}

func NewOAuthManager() *manage.Manager {
	return manager
}
